﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;
using System.Collections.Generic; 
using Ccit.Global;
using System.Collections; 
using System.Text;
using Ccit.OA.MsGroup;
namespace Ccit.OA.Group
{
    public class GroupSQL:IGroup
    {
        #region 获取分组信息tree
        /// <summary>
        /// 获取分组信息tree
        /// </summary>
        /// <returns></returns>
        public string GetGroupMes(string Query)
        {
            string WhereClase ="";
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            if (Query == "0")
                WhereClase = " and GRP_A1_20="+loginUser.UserId;
            StringBuilder sbXML = new StringBuilder("<ADDRS>");
            string cmdText = "select GRP_A1_10,GRP_A1_30 from GRP_A1 where GRP_A1_40 ="+Query+" "+WhereClase+"";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    sbXML.Append("<ADDR><GROUPID>").Append(iReader["GRP_A1_10"].ToString()).Append("</GROUPID>");
                    sbXML.Append("<GROUPNAME><![CDATA[").Append(iReader["GRP_A1_30"].ToString()).Append("]]></GROUPNAME>");
                    sbXML.Append("</ADDR>");
                }
            }
            sbXML.Append("</ADDRS>");
            return sbXML.ToString();
        } 
        #endregion

        #region 保存分组信息
        /// <summary>
        /// 保存分组信息
        /// </summary>
        /// <returns></returns>
        public List<string> DoSaveGroupMes(MsGroupMes ms)
        {
            List<string> lstResult = new List<string>();
            string curYear = DateTime.Now.ToString("yyyy-MM-dd");
            string cmdText = "";
            string sUserId = "0";
            if (ms.ID == "")
            {
                LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
                ms.ID = DataInterface.GetMaxId("GRP_A1", "GRP_A1_10").ToString();
                if (ms.GroupType == "0")
                {
                    sUserId = loginUser.UserId;
                }
                cmdText = "insert into GRP_A1 values(" + ms.ID + "," + sUserId + ",'" + ms.GroupName + "'," + ms.GroupType + "," +
                    "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
            }
            else
            {
                cmdText = "update GRP_A1 set GRP_A1_30='" + ms.GroupName + "' where GRP_A1_10="+ms.ID;
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                lstResult.Add(ms.ID);
                lstResult.Add(ms.GroupName);
                lstResult.Add("数据保存成功!");
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("");
                lstResult.Add("数据保存失败，请尝试重新保存!");
            }
            return lstResult;
        }

        #endregion

        #region 获取编辑页面信息
        /// <summary>
        /// 获取编辑页面信息
        /// </summary>
        /// <param name="sRecId"></param>
        /// <returns></returns>
        public MsGroupMes GetGroupMesInfo(string sRecId)
        {
            string cmdText = "select GRP_A1_10,GRP_A1_30 from GRP_A1 where GRP_A1_10="+sRecId;
            MsGroupMes ms = new MsGroupMes();
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if(iReader.Read())
                {
                    ms.ID = iReader["GRP_A1_10"].ToString();
                    ms.GroupName = iReader["GRP_A1_30"].ToString();
                }
            }
            return ms;
        }

        #endregion

        #region 删除分组信息
        /// <summary>
        /// 删除分组信息
        /// </summary>
        /// <param name="sRecId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<string> DoOperate(string sRecId,string UserId, string type)
        {
            string cmdText = "";
            List<string> lstResult = new List<string>();
            if (type == "0")
            {
                cmdText = "delete GRP_A1 where GRP_A1_10 =" + sRecId;
                cmdText += "delete GRP_A2 where GRP_A2_10 in(" + sRecId + ")";
            }
            else
            {
                cmdText = "delete GRP_A2 where GRP_A2_10 =" + sRecId + " and GRP_A2_20 in("+UserId+")";
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                lstResult.Add(sRecId);
                lstResult.Add("删除成功!");
            }
            else
            {
                lstResult.Add("-1");
                lstResult.Add("操作失败:服务器忙,请稍后再试!");
            }
            return lstResult;
        }

        #endregion

        #region 保存分组详细信息
        /// <summary>
        /// 保存分组详细信息
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveDetail(MsGroupDetail ms)
        {
            string[] lstDetail = ms.UserId.Split(',');
            string cmdText = "";
            List<string> lstResult = new List<string>();
            if(lstDetail.Length>0)
            {
                cmdText = "delete GRP_A2 where GRP_A2_10 in(" + ms.GroupId + ")";
                for (int i = 0; i < lstDetail.Length;i++ )
                {
                    cmdText += "insert into GRP_A2 values("+ms.GroupId+","+lstDetail[i]+")";
                }
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                lstResult.Add(ms.GroupId);
                lstResult.Add("操作成功!");
            }
            else
            {
                lstResult.Add("-1");
                lstResult.Add("操作失败:服务器忙,请稍后再试!");
            }
            return lstResult;
        }

        #endregion

        #region 获取分组明细
        /// <summary>
        /// 获取分组明细
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        public DataSet GetGroupDetailMes(string GroupId)
        {
            string cmdText = "select GRP_A2_20  as PKID,  userName,deptName,postionname as postName from GRP_A2 ";
            cmdText += " left join v_udp on GRP_A2_20 = userid where  GRP_A2_10="+GroupId;
            DataSet dSet = DataAccessor.ExecuteDataSet(cmdText);
            return dSet;
        }

        #endregion
    }
}
